package nanguo.not.cloud.controller;

import io.swagger.annotations.ApiOperation;
import nanguo.not.cloud.dto.UserAddDto;
import nanguo.not.cloud.entity.Dept;
import nanguo.not.cloud.entity.Role;
import nanguo.not.cloud.entity.User;
import nanguo.not.cloud.service.DeptService;
import nanguo.not.cloud.service.RoleService;
import nanguo.not.cloud.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: jing
 * @Date: 2019/3/28 16:01
 * @Version 1.0
 */
@RestController

public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private RoleService roleService;


    @PostMapping("user/save")
    @ApiOperation(value = "保存用户")
    public Boolean save(@RequestBody UserAddDto dto){
        User user = new User();
        BeanUtils.copyProperties(dto,user);
        Dept dept = deptService.getInfo();
        Role role = roleService.findAll().get(0);
        user.setDept(dept);
        user.setRole(role);
        userService.save(user);
        return true;
    }


    @GetMapping("user/delete/{id}")
    @ApiOperation(value = "删除用户")
    public Boolean save(@PathVariable Long id){
        userService.delete(id);
        return true;
    }

    @GetMapping("role/delete/{id}")
    public Boolean deleteRole(@PathVariable Long id){
        roleService.delete(id);
        return true;
    }

}
